package com.h52mm.curator;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.data.Stat;

public class GetCuratorNodeSample {

    public static void main(String[] args) throws Exception {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.builder()
                .connectString("127.0.0.1:2182") //server地址
                .sessionTimeoutMs(50000) // 会话超时时间
                .connectionTimeoutMs(30000) // 连接超时时间
                .retryPolicy(retryPolicy) // 重试策略
                .namespace("curator") // 独⽴命名空间/base
                .build(); //
        client.start();
        System.out.println("Zookeeper session established");

        //添加节点
        String path = "/test_get";
        client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path,"init".getBytes());
        System.out.println("success create znode"+path);
        //获取节点数据
        Stat stat = new Stat();
        byte[] bytes = client.getData().storingStatIn(stat).forPath(path);
        System.out.println(new String(bytes));

    }

}
